package com.cn.medicalcare.controller.servlet;

import com.cn.medicalcare.controller.servlettool.MySessionContext;
import com.cn.medicalcare.customtool.JsonTool;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;

@WebServlet("/userinfo.json")
public class UserInfoServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String sessionId=req.getParameter("sessionId");
        if(sessionId!=null && sessionId.length()>0){
            HttpSession session= MySessionContext.getSession(sessionId);
            Map<String,String> map=new LinkedHashMap<>();
            if (session!=null){
                map.put("status","200");
                map.put("username",(String) session.getAttribute("username"));
            } else {
                map.put("status","404");
            }
            resp.getWriter().print(JsonTool.toJson(map));
        }
    }
}
